<?php

namespace App\Http\Controllers\Network;

use App\Http\Controllers\Controller;
use App\Models\AdminUserModel;
use App\Models\yb\GoodsModel;
use App\Models\yb\GoodsOrderModel;
use App\Models\yb\GoodsTypeModel;
use App\Models\yb\NetworkGoodsModel;
use App\Models\yb\NetworkGoodsOrderModel;
use App\Models\yb\NetworkGoodsOrderDetailModel;
use App\Models\yb\NetworkCreditRecordModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use EasyWeChat\Factory;
use Overtrue\Socialite\Providers\AbstractProvider;



class AccountController extends Controller
{

    /**
     * Get a JWT via given credentials.
     *  创建账号
     * @return \Illuminate\Http\JsonResponse
     */
    public function Add(Request $request)
    {
        $openid = $request->post('openid');

        $user_data = AdminUserModel::where(['openid' => $openid])->first();

        if (empty($user_data) || empty($openid)) {
            return response()->json(['code' => '401', 'msg' => '请登录']);
        }
        
        $password = $request->post('password');
        if(empty($password)){
              return response()->json(['code' => '201', 'msg' => '密码不能为空']);
        }
        
        $name = $request->post('name');
        if(empty($name)){
              return response()->json(['code' => '201', 'msg' => '员工名称不能为空']);
        }
        
        $mobile = $request->post('mobile');
        if(empty($mobile)){
              return response()->json(['code' => '201', 'msg' => '员工手机号不能为空']);
        }
        
        
        $admin_user = AdminUserModel::where(['username'=>$mobile])->first();
        if(!empty($admin_user)){
            return response()->json(['code' => '201', 'msg' => '该手机号已被注册']);
        }
        
        $password = $request->post('password');

        AdminUserModel::insert([
            'username' => $mobile,
            'name' => $name,
            'mobile' => $mobile,
            'password' => Hash::make($password),
            'password_text' => $password,
            'type' => 3,
            'admin_id' => $user_data['id']
        ]);

        return response()->json(['code'=>'200','msg' => '请求成功']);

    }
    
    
     /**
     * Get a JWT via given credentials.
     *  账号列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function Listing(Request $request)
    {
        $openid = $request->post('openid');

        $user_data = AdminUserModel::where(['openid' => $openid])->first();

        if (empty($user_data) || empty($openid)) {
            return response()->json(['code' => '401', 'msg' => '请登录']);
        }
        
        $page = $request->post('page', 1);

        $take = $request->post('take', 20);
        
        $data = AdminUserModel::where(['admin_id'=>$user_data['id']])->select(['id','name','mobile','password_text','status'])->orderBy('created_at','desc')->skip(($page - 1) * $take)->take($take)->get();

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);

    }
    
    
     /**
     * Get a JWT via given credentials.
     *  账号编辑
     * @return \Illuminate\Http\JsonResponse
     */
    public function Edit(Request $request)
    {
        $openid = $request->post('openid');

        $user_data = AdminUserModel::where(['openid' => $openid])->first();

        if (empty($user_data) || empty($openid)) {
            return response()->json(['code' => '401', 'msg' => '请登录']);
        }
        
        $id = $request->post('id');
        
        $status = $request->post('status');
        
        if($status != ''){
            
            AdminUserModel::where(['id' => $id])->update([
                'status' => $status
            ]);

            return response()->json(['code'=>'200','msg' => '请求成功']);
        }else{

            $admin_user_data = AdminUserModel::where(['id' => $id])->first();
            if(empty($admin_user_data)){
                return response()->json(['code' => '201', 'msg' => '该账号不可编辑']);
            }
            
            $password = $request->post('password');
            if(empty($password)){
                  return response()->json(['code' => '201', 'msg' => '密码不能为空']);
            }
            
            $name = $request->post('name');
            if(empty($name)){
                  return response()->json(['code' => '201', 'msg' => '员工名称不能为空']);
            }
            
            $mobile = $request->post('mobile');
            if(empty($mobile)){
                  return response()->json(['code' => '201', 'msg' => '员工手机号不能为空']);
            }
            
            $status = $request->post('status');

            
            AdminUserModel::where(['id' => $id])->update([
                'username' => $mobile,
                'name' => $name,
                'status' => $status,
                'password' => Hash::make($password),
                'mobile' => $mobile,
            ]);
    
            return response()->json(['code'=>'200','msg' => '请求成功']); 
        }

    }
    
      /**
     * Get a JWT via given credentials.
     *  账号删除
     * @return \Illuminate\Http\JsonResponse
     */
    public function Delete(Request $request)
    {
        $openid = $request->post('openid');

        $user_data = AdminUserModel::where(['openid' => $openid])->first();

        if (empty($user_data) || empty($openid)) {
            return response()->json(['code' => '401', 'msg' => '请登录']);
        }
        
        $id = $request->post('id');

        $admin_user_data = AdminUserModel::where(['id' => $id])->first();
        if(empty($admin_user_data)){
            return response()->json(['code' => '201', 'msg' => '该账号已被删除']);
        }
        
        AdminUserModel::where(['id' => $id,'admin_id'=>$user_data['id']])->delete();

        return response()->json(['code'=>'200','msg' => '请求成功']);

    }
    
          /**
     * Get a JWT via given credentials.
     *  绑定微信公众平台
     * @return \Illuminate\Http\JsonResponse
     */
    public function wechatBinding(Request $request)
    {
        $openid = $request->post('openid');
        if(empty($openid)){
              return response()->json(['code' => '201', 'msg' => '未授权']);
        }
      
        $mobile = $request->post('mobile');
        if(empty($mobile)){
              return response()->json(['code' => '201', 'msg' => '员工手机号不能为空']);
        }

        $password = $request->post('password');
        if(empty($password)){
              return response()->json(['code' => '201', 'msg' => '密码不能为空']);
        }
        
        $admin_data = AdminUserModel::where(['username'=>$mobile])->first();
        
        if(empty($admin_data)){
            return response()->json(['code' => '201', 'msg' => '手机号未注册']);
        }
        
        if(!Hash::check($password,$admin_data['password'])){
            return response()->json(['code'=>'201','msg' => '原密码错误']);
        }
        
        AdminUserModel::where(['username'=>$mobile])->update([
            'wechat_openid'=>$openid,    
        ]);
      

        return response()->json(['code'=>'200','msg' => '请求成功']);

    }
    
    public function wechatBindingView(Request $request)
    {
        $config = [
            'app_id' => 'wx3cfd9698645432e9',
            'secret' => '0b5b897aa5751aa1e414ce1a15d6d6d5',
            'token' => 'C1089F8BE36698A4EB575EDBB5919B60',
            'response_type' => 'array',
            'aes_key' => '461wQLRvxu4j7nGfm0FASGHFpTY1lm2JfJyWgzqWTgP',
            'oauth' => [
                'scopes'   => ['snsapi_userinfo'],
                'callback' => '/api/network/wechat/oauth/callback',
            ],
        ];
        
        $app = Factory::officialAccount($config);
        
        $oauth = $app->oauth;
        
        // 未登录
        if (empty($_SESSION['wechat_user'])) {
        
          $_SESSION['target_url'] = 'user/profile';
        
          return $oauth->redirect();
          // 这里不一定是return，如果你的框架action不是返回内容的话你就得使用
          // $oauth->redirect()->send();
        }
        
        // 已经登录过
        $user = $_SESSION['wechat_user'];

    }
    
    public function wechatOauthCallback(Request $request)
    {
        $config = [
            'app_id' => 'wx3cfd9698645432e9',
            'secret' => '0b5b897aa5751aa1e414ce1a15d6d6d5',
            'token' => 'C1089F8BE36698A4EB575EDBB5919B60',
            'response_type' => 'array',
            'aes_key' => '461wQLRvxu4j7nGfm0FASGHFpTY1lm2JfJyWgzqWTgP',
            'oauth' => [
                'scopes'   => ['snsapi_userinfo'],
                'callback' => '/api/network/wechat/oauth/callback',
            ],
        ];
        
        $app = Factory::officialAccount($config);
        
        $oauth = $app->oauth;
        
        $user = $oauth->user();
      
        $data = $user->toArray();

        header('location:'. "/wechat/#/pages/index/index?openid=".$data['original']['openid']);
        // header('location:'. "/wechat/index.html?openid=".$data['original']['openid']);

        
    }
}
